/**
*  
*   @Description    抓取及时新闻列表文章链接,根据链接调用抓取文章内容类(CrawlArticleContent)
*   @creator         tangkun
*   @create-time     2011-7-15
*   @revision        $Id
*/

package com.scrawler.html.template;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.log4j.Logger;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.scrawler.tool.SimpleBloomFilter;
import com.scrawler.vo.NewsContent;

public class SecondContentCrawlerPool{
	
	ExecutorService exec = null;
	static int count = 0;
	
	Logger logger = Logger.getLogger(SecondContentCrawlerPool.class);
	private static int runningThreadNum = 0;			// 统计线程池中还在运行的线程数目
	
	private static final SecondContentCrawlerPool indexPool = new SecondContentCrawlerPool();
	
	/*
	 * 构造函数,初始化相关参数
	 * 
	 * */
	private SecondContentCrawlerPool()
	{					
		exec = Executors.newFixedThreadPool(20);	// 开启20个线程来抓取并且挖掘文章内容
	}
	
	public static SecondContentCrawlerPool getInstance(){
		return indexPool;
	}
	
	
	public void addTask(Runnable task){
		exec.execute(task);
	}
	
	
	
	/**
	 * 获取统计的还在运行线程数
	 * @return 
	 */
	
	public static synchronized int getRunningThreadNum()
	{
		return runningThreadNum;	
	}
	
	/**
	 * 把线程池中正在运行的线程数目加+1
	 * 同步函数
	 */
	public synchronized static void increaseRunningThreadNums()
	{
		runningThreadNum ++;		//运行的线程数目加+1
	}
	
	/**
	 * 统计数字,把线程池中的运行线程数目-1
	 * 同步函数 
	 */
	public synchronized static void decreaseRunningThreadNums()
	{
		runningThreadNum --;		// 运行的线程数目减-1
	}
	
	
	/*
	 *  从url中提取出文章id
	 * 	输入: url,文章的地址
	 *  输出: 文章id
	 *  形如: http://news.ifeng.com/detail_2011_07/22/7859423_0.shtml
	 */
	public String cutUrlId(String url) {
		logger.info(url);
		int startIndex = url.lastIndexOf("/");	//定位到最后一级
		int endIndex = url.lastIndexOf("_");	// 定位到_
		if (endIndex >= (startIndex + 1))		// 不是指向同一位
			return url.substring(startIndex + 1, endIndex);	//截取
		else
			return null;
	}
	
	/**
	 * 测试代码
	 * @param args
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SecondContentCrawlerPool test = new SecondContentCrawlerPool();
		// System.out.println(
	}
}
